package com.example.h1nson.realphotography.viewpager;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

import com.example.h1nson.realphotography.R;
import com.example.h1nson.realphotography.adapter.SearchAdapter;
import com.example.h1nson.realphotography.asyncTask.SearchAsynTask;
import com.example.h1nson.realphotography.interface_photo.OnDataFinishedListener;
import com.example.h1nson.realphotography.interface_photo.OnPushItemListener;
import com.example.h1nson.realphotography.interface_photo.OnSearchItemListener;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by H1nson on 2016/5/4.
 */
public class MainTab02 extends Fragment {
    private int[] user,photo,type;
    private ImageView ivDeleteText;
    private EditText etSearch;
    private Button btnSearch;
    private ListView listView;
    private LinkedList<String> mListItems;
    private String[] mStrings = {};
    private ArrayAdapter<String> adapter;
    private OnSearchItemListener onSearchItemListener;
    public MainTab02() {
    }
    public void setOnSearchItemListener(OnSearchItemListener onSearchItemListener){
        this.onSearchItemListener = onSearchItemListener;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view =  inflater.inflate(R.layout.main_tab_02, container, false);
        mListItems = new LinkedList<String>();
        mListItems.addAll(Arrays.asList(mStrings));
        adapter = new ArrayAdapter<String>(inflater.getContext(),
                android.R.layout.simple_list_item_1, mListItems);
        ivDeleteText = (ImageView) view.findViewById(R.id.ivDeleteText);
        btnSearch = (Button)view.findViewById(R.id.btnSearch);
        etSearch = (EditText) view.findViewById(R.id.etSearch);
        listView =(ListView)view.findViewById(R.id.tab02_listview);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(position<user.length) {
                    onSearchItemListener.goDetail(type[position],user[position]);
                }else{
                    onSearchItemListener.goDetail(type[position],photo[position-user.length]);
                }
            }
        });
        btnSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String searchContent = etSearch.getText().toString();
                mListItems.clear();

                if(searchContent!=null) {
                    btnSearch.setEnabled(false);
                    SearchAsynTask searchAsynTask = new SearchAsynTask(searchContent);
                    searchAsynTask.setOnDataFinishedListener(new OnDataFinishedListener() {
                        @Override
                        public void onDataSuccessfully(Object data) {
                            try {
                                JSONObject jsonObject = new JSONObject(data.toString());
                                int userSize = jsonObject.getInt("userSize");
                                int photoSize = jsonObject.getInt("photoSize");
                                user=new int[userSize];
                                photo= new int[photoSize];

                                type = new int[userSize+photoSize];

                                for(int i=0;i<userSize;i++){
                                    user[i]=jsonObject.getInt("userId"+i);
                                    mListItems.addLast("(人)"+jsonObject.getString("userName"+i));
                                    type[i]=0;
                                }
                                for(int i=0;i<photoSize;i++){
                                    photo[i]=jsonObject.getInt("photoId"+i);
                                    mListItems.addLast("(地)"+jsonObject.getString("photoAddress"+i));
                                    type[userSize+i]=1;
                                }
                            }catch (JSONException e){
                                e.printStackTrace();
                            }
                            adapter.notifyDataSetChanged();
                            btnSearch.setEnabled(true);
                        }

                        @Override
                        public void onDataFailed() {
                            btnSearch.setEnabled(true);
                        }
                    });
                    searchAsynTask.execute();
                }
            }
        });
        ivDeleteText.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                etSearch.setText("");
            }
        });

        etSearch.addTextChangedListener(new TextWatcher() {

            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub

            }

            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
                // TODO Auto-generated method stub

            }

            public void afterTextChanged(Editable s) {
                if (s.length() == 0) {
                    ivDeleteText.setVisibility(View.GONE);
                } else {
                    ivDeleteText.setVisibility(View.VISIBLE);
                }
            }
        });

        return view;
    }
}